package space.xxhui.principleof6.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;

import space.xxhui.principleof6.interfaces.IImageCache;

/**
 * Created by hui on 2016/10/19.
 */

public class ImageAsyncTask extends AsyncTask<String,Void,Bitmap> {
    private ImageView imageView;
    private Context context;
    private IImageCache iImageCache;


    public ImageAsyncTask(IImageCache iImageCache,ImageView imageView, Context context ) {
        this.imageView = imageView;
        this.context  = context;
        this.iImageCache = iImageCache;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),Integer.parseInt(params[0]));
        iImageCache.setBitmap(params[0],bitmap);
        Bitmap lruBitmap = iImageCache.getBitmap(params[0]);
        return lruBitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        super.onPostExecute(bitmap);
        imageView.setImageBitmap(bitmap);
    }
}
